package com.vanzo.talkie;

import android.app.Activity;
import android.graphics.Color;
import android.view.View;
import android.widget.TextView;

import com.vanzo.talkie2.ColorTextView;
import com.vanzo.talkie2.R;
import com.vanzo.talkie2.TriplePicker;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tisen on 2018/1/16.
 */

public class FrPicker {

    private static final String TAG = "FrequencyPicker";
    private static List<String> numList1;
    private static List<String> numList2;
    private static List<String> numList3;

    static {
        numList1 = new ArrayList<>();
        numList2 = new ArrayList<>();
        numList3 = new ArrayList<>();
        for (int i = 0; i <= 70; i++) {
            numList1.add(400 + i + "");
        }
        for (int i = 0; i < 10; i++) {
            numList2.add(i + "");
        }
        numList3.add("000");
        numList3.add("125");
        numList3.add("250");
        numList3.add("375");
        numList3.add("500");
        numList3.add("625");
        numList3.add("750");
        numList3.add("875");
    }

    private TriplePicker picker;
    private TextView textView;
    private Activity activity;
    private String front;
    private String defaultValue;

    public FrPicker(Activity activity, TextView textView, String front, String defaultValue) {
        this.activity = activity;
        this.textView = textView;
        this.front = front;
        this.textView.setText(front + ":" + defaultValue);
        this.defaultValue = defaultValue;
//        this.textView.setOnClickListener(textViewListener);
    }

    public FrPicker(Activity activity, TextView textView, String defaultValue) {
        this.activity = activity;
        this.textView = textView;
        this.front = "hehe";
        this.textView.setText(front + ":" + defaultValue);
        this.defaultValue = defaultValue;
//        this.textView.setOnClickListener(textViewListener);
    }

    private View.OnClickListener textViewListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (picker == null) {
                picker = new TriplePicker(activity, numList1, numList2, numList3);
                picker.setFirstLabel(null, ".");
                picker.setTitleText("设置" + front + "频率");
                picker.setTopLineColor(Color.TRANSPARENT);
                picker.setTitleTextColor(activity.getResources().getColor(R.color.main_activity_background_color));
                picker.setSubmitTextColor(activity.getResources().getColor(R.color.wheel_pop_text_color_focus));
                picker.setTextColor(activity.getResources().getColor(R.color.wheel_pop_text_color_focus), activity.getResources().getColor(R.color.wheel_pop_text_color_normal));
                picker.setAnimationStyle(R.style.Picker);
                picker.setOnPickListener(new TriplePicker.OnPickListener() {
                    @Override
                    public void onPicked(int var1, int var2, int var3) {
                        textView.setText(numList1.get(var1) + "." + numList2.get(var2) + numList3.get(var3));
                        if (pickListener != null) {
                            pickListener.onPick();
                        }
                    }
                });
                picker.setOnWheelListener(new TriplePicker.OnWheelListener() {
                    @Override
                    public void onFirstWheeled(int var1, String var2) {
                        if (var1 == 70) {
                            picker.setSelectedSecondIndex(0);
                            picker.setSelectedThirdIndex(0);
                        }
                    }

                    @Override
                    public void onSecondWheeled(int var1, String var2) {
                        if (var1 != 0 && picker.getSelectedFirstIndex() == 70) {
                            picker.setSelectedFirstIndex(69);
                        }
                    }

                    @Override
                    public void onThirdWheeled(int var1, String var2) {
                        if (var1 != 0 && picker.getSelectedFirstIndex() == 70) {
                            picker.setSelectedFirstIndex(69);
                        }
                    }
                });
            }
            picker.show();
            String value = textView.getText().toString();
            picker.setSelectedIndex(numList1.indexOf(value.substring(0, 3)), numList2.indexOf(value.substring(4, 5)), numList3.indexOf(value.substring(5, 8)));
        }
    };

    public void show() {
        if (picker == null) {
            picker = new TriplePicker(activity, numList1, numList2, numList3);
            picker.setFirstLabel(null, ".");
            picker.setTitleText("设置" + front + "功率");
            picker.setTopLineColor(Color.TRANSPARENT);
            picker.setTitleTextColor(activity.getResources().getColor(R.color.main_activity_background_color));
            picker.setSubmitTextColor(activity.getResources().getColor(R.color.wheel_pop_text_color_focus));
            picker.setTextColor(activity.getResources().getColor(R.color.wheel_pop_text_color_focus), activity.getResources().getColor(R.color.wheel_pop_text_color_normal));
            picker.setAnimationStyle(R.style.Picker);
            picker.setOnPickListener(new TriplePicker.OnPickListener() {
                @Override
                public void onPicked(int var1, int var2, int var3) {
                    textView.setText(numList1.get(var1) + "." + numList2.get(var2) + numList3.get(var3));
                    if (pickListener != null) {
                        pickListener.onPick();
                    }
                }
            });
            picker.setOnWheelListener(new TriplePicker.OnWheelListener() {
                @Override
                public void onFirstWheeled(int var1, String var2) {
                    if (var1 == 70) {
                        picker.setSelectedSecondIndex(0);
                        picker.setSelectedThirdIndex(0);
                    }
                }

                @Override
                public void onSecondWheeled(int var1, String var2) {
                    if (var1 != 0 && picker.getSelectedFirstIndex() == 70) {
                        picker.setSelectedFirstIndex(69);
                    }
                }

                @Override
                public void onThirdWheeled(int var1, String var2) {
                    if (var1 != 0 && picker.getSelectedFirstIndex() == 70) {
                        picker.setSelectedFirstIndex(69);
                    }
                }
            });
        }
        picker.show();
        String value = textView.getText().toString();
        picker.setSelectedIndex(numList1.indexOf(value.substring(0, 3)), numList2.indexOf(value.substring(4, 5)), numList3.indexOf(value.substring(5, 8)));
    }

    private PickListener pickListener;

    public void setPickListener(PickListener pickListener) {
        this.pickListener = pickListener;
    }

    interface PickListener {
        void onPick();
    }
}
